package red.stu.wechat.factory.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import me.xuxiaoxiao.chatapi.wechat.entity.message.WXText;
import red.stu.wechat.factory.robot.WeChatRobot;
import red.stu.wechat.factory.robot.WeChatRobotFactory;
import red.stu.wechat.factory.robot.WeChatRoobotCallback;
import red.stu.wechat.factory.robot.json.WechatCreateResultJson;
import red.stu.wechat.factory.robot.json.WechatResultJson;
import red.stu.wechat.factory.robot.json.WechatResultWXTextJson;
import red.stu.wechat.factory.util.WeChatLoginSign;

@RestController
@RequestMapping(value = "/wechat")
public class WeChatAppController {

	@Value("${charset}")
	private String wechatCharset;

	@Autowired
	private WeChatRobotFactory wechatRobotFactory;

	@Autowired
	private WeChatLoginSign wechatLoginSign;

	/**
	 * 校验身份，生成临时签名
	 * 
	 * @param session
	 * @param sign
	 *            loginkey应用启动配置的key
	 * @return
	 */
	@RequestMapping("/sign/{sign}")
	public WechatResultJson sign(@PathVariable("sign") String sign) {
		boolean result = wechatLoginSign.sign().equals(sign);
		return WechatResultJson.builder(result);
	}

	/**
	 * 创建微信机器人
	 * 
	 * @param session
	 * @param sign
	 *            临时数据签名
	 * @param requestTime
	 *            请求时间(用于参与临时数据签名)
	 * @param callbackUrl
	 *            接口回调地址
	 * @param charset
	 *            字符编码
	 * @return
	 */
	@RequestMapping("/create/{sign}/{requestTime}")
	public WechatCreateResultJson createWechatApp(@PathVariable("sign") String sign,
			@PathVariable("requestTime") String requestTime, @RequestParam("callbackUrl") String callbackUrl,
			@RequestParam(value = "charset", required = false) String charset) {
		System.out.println(String.format("create > sign:%s requestTime:%s callbackUrl:%s charset:%s", sign, requestTime,
				callbackUrl, charset));
		WechatCreateResultJson resultJson = WechatResultJson.builder(new WechatCreateResultJson());
		// check sign
		if (!wechatLoginSign.check(sign, requestTime, callbackUrl)) {
			return resultJson.setSuccess(false);
		}
		// default charset
		if (charset == null)
			charset = wechatCharset;
		// create WeChatRobot
		WeChatRobot wechatRobot = wechatRobotFactory.builder(new WeChatRoobotCallback(sign, callbackUrl, charset));
		return resultJson.setRid(wechatRobot.rid).setSuccess(true);
	}

	/**
	 * 启动微信机器人 (只能启动一次)
	 * 
	 * @param sign
	 * @param requestTime
	 * @param rid
	 * @return
	 */
	@RequestMapping("/startup/{sign}/{requestTime}")
	public WechatResultJson startup(@PathVariable("sign") String sign, @PathVariable("requestTime") String requestTime,
			@RequestParam("rid") String rid) {
		WeChatRobot robot = wechatRobotFactory.getRobot(rid);
		robot.client.startup();
		return WechatResultJson.builder(robot != null);
	}

	/**
	 * 关闭微信机器人
	 * 
	 * @param sign
	 * @param requestTime
	 * @param rid
	 * @return
	 */
	@RequestMapping("/shutdown/{sign}/{requestTime}")
	public WechatResultJson shutdown(@PathVariable("sign") String sign, @PathVariable("requestTime") String requestTime,
			@RequestParam("rid") String rid) {
		WeChatRobot robot = wechatRobotFactory.getRobot(rid);
		robot.client.shutdown();
		return WechatResultJson.builder(robot != null);
	}

	/**
	 * 发送消息
	 * 
	 * @param session
	 * @param sign
	 *            临时数据签名
	 * @param requestTime
	 *            请求时间(用于参与临时数据签名)
	 * @param rid
	 *            机器人ID
	 * @param toContactId
	 *            联络ID
	 * @param text
	 *            发送消息内容
	 * @return
	 */
	@RequestMapping("/sendText/{sign}/{requestTime}/{rid}/{toContactId}")
	public WechatResultWXTextJson sendText(@PathVariable("sign") String sign,
			@PathVariable("requestTime") String requestTime, @PathVariable("rid") String rid,
			@PathVariable("toContactId") String toContactId, @RequestParam("text") String text) {
		System.out.println(String.format("sendText> sign:%s requestTime:%s rid:%s toContactId:%s text:%s", sign,
				requestTime, rid, toContactId, text));
		WechatResultWXTextJson resultJson = WechatResultJson.builder(new WechatResultWXTextJson());
		// check sign
		if (!wechatLoginSign.check(sign, requestTime, rid, toContactId, text)) {
			return resultJson.setSuccess(false);
		}
		// WeChatRobot sendMessage
		WeChatRobot robot = wechatRobotFactory.getRobot(rid);
		WXText result = robot.client.sendText(robot.client.userContact(toContactId), text);
		return resultJson.setWxText(result).setSuccess(true);
	}

}
